package com.ruoyi.report_sj.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_sj.domain.bo.ReportSjphbsjBaDetailBo;
import com.ruoyi.report_sj.domain.vo.ReportSjphbsjBaDetailVo;
import com.ruoyi.report_sj.domain.ReportSjphbsjBaDetail;
import com.ruoyi.report_sj.mapper.ReportSjphbsjBaDetailMapper;
import com.ruoyi.report_sj.service.IReportSjphbsjBaDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 砂浆配合比设计试验检测记录详情Service业务层处理
 *
 * @author zyb
 * @date 2024-05-29
 */
@RequiredArgsConstructor
@Service
public class ReportSjphbsjBaDetailServiceImpl implements IReportSjphbsjBaDetailService {

    private final ReportSjphbsjBaDetailMapper baseMapper;

    /**
     * 查询砂浆配合比设计试验检测记录详情
     */
    @Override
    public ReportSjphbsjBaDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询砂浆配合比设计试验检测记录详情列表
     */
    @Override
    public TableDataInfo<ReportSjphbsjBaDetailVo> queryPageList(ReportSjphbsjBaDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportSjphbsjBaDetail> lqw = buildQueryWrapper(bo);
        Page<ReportSjphbsjBaDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询砂浆配合比设计试验检测记录详情列表
     */
    @Override
    public List<ReportSjphbsjBaDetailVo> queryList(ReportSjphbsjBaDetailBo bo) {
        LambdaQueryWrapper<ReportSjphbsjBaDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportSjphbsjBaDetail> buildQueryWrapper(ReportSjphbsjBaDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportSjphbsjBaDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExcelId() != null, ReportSjphbsjBaDetail::getExcelId, bo.getExcelId());
        lqw.eq(bo.getBianh() != null, ReportSjphbsjBaDetail::getBianh, bo.getBianh());
        lqw.eq(StringUtils.isNotBlank(bo.getShejqddj()), ReportSjphbsjBaDetail::getShejqddj, bo.getShejqddj());
        lqw.eq(StringUtils.isNotBlank(bo.getQuybw()), ReportSjphbsjBaDetail::getQuybw, bo.getQuybw());
        lqw.eq(bo.getChengxrq() != null, ReportSjphbsjBaDetail::getChengxrq, bo.getChengxrq());
        lqw.eq(bo.getKangyrq() != null, ReportSjphbsjBaDetail::getKangyrq, bo.getKangyrq());
        lqw.eq(StringUtils.isNotBlank(bo.getLingq()), ReportSjphbsjBaDetail::getLingq, bo.getLingq());
        lqw.eq(StringUtils.isNotBlank(bo.getShijcc()), ReportSjphbsjBaDetail::getShijcc, bo.getShijcc());
        lqw.eq(StringUtils.isNotBlank(bo.getChengymj()), ReportSjphbsjBaDetail::getChengymj, bo.getChengymj());
        lqw.eq(bo.getHuansxs() != null, ReportSjphbsjBaDetail::getHuansxs, bo.getHuansxs());
        lqw.eq(bo.getShz2() != null, ReportSjphbsjBaDetail::getShz2, bo.getShz2());
        lqw.eq(bo.getSqd2() != null, ReportSjphbsjBaDetail::getSqd2, bo.getSqd2());
        lqw.eq(bo.getSpjqd2() != null, ReportSjphbsjBaDetail::getSpjqd2, bo.getSpjqd2());
        lqw.eq(bo.getShz3() != null, ReportSjphbsjBaDetail::getShz3, bo.getShz3());
        lqw.eq(bo.getSqd3() != null, ReportSjphbsjBaDetail::getSqd3, bo.getSqd3());
        lqw.eq(bo.getSpjqd3() != null, ReportSjphbsjBaDetail::getSpjqd3, bo.getSpjqd3());
        lqw.eq(bo.getQhz1() != null, ReportSjphbsjBaDetail::getQhz1, bo.getQhz1());
        lqw.eq(bo.getQqd1() != null, ReportSjphbsjBaDetail::getQqd1, bo.getQqd1());
        lqw.eq(bo.getQpjqd1() != null, ReportSjphbsjBaDetail::getQpjqd1, bo.getQpjqd1());
        lqw.eq(bo.getQhz2() != null, ReportSjphbsjBaDetail::getQhz2, bo.getQhz2());
        lqw.eq(bo.getQqd2() != null, ReportSjphbsjBaDetail::getQqd2, bo.getQqd2());
        lqw.eq(bo.getQpjqd2() != null, ReportSjphbsjBaDetail::getQpjqd2, bo.getQpjqd2());
        lqw.eq(bo.getQhz3() != null, ReportSjphbsjBaDetail::getQhz3, bo.getQhz3());
        lqw.eq(bo.getQqd3() != null, ReportSjphbsjBaDetail::getQqd3, bo.getQqd3());
        lqw.eq(bo.getQpjqd3() != null, ReportSjphbsjBaDetail::getQpjqd3, bo.getQpjqd3());
        lqw.eq(bo.getEhz1() != null, ReportSjphbsjBaDetail::getEhz1, bo.getEhz1());
        lqw.eq(bo.getEqd1() != null, ReportSjphbsjBaDetail::getEqd1, bo.getEqd1());
        lqw.eq(bo.getEpjqd1() != null, ReportSjphbsjBaDetail::getEpjqd1, bo.getEpjqd1());
        lqw.eq(bo.getEhz2() != null, ReportSjphbsjBaDetail::getEhz2, bo.getEhz2());
        lqw.eq(bo.getEqd2() != null, ReportSjphbsjBaDetail::getEqd2, bo.getEqd2());
        lqw.eq(bo.getEpjqd2() != null, ReportSjphbsjBaDetail::getEpjqd2, bo.getEpjqd2());
        lqw.eq(bo.getEhz3() != null, ReportSjphbsjBaDetail::getEhz3, bo.getEhz3());
        lqw.eq(bo.getEqd3() != null, ReportSjphbsjBaDetail::getEqd3, bo.getEqd3());
        lqw.eq(bo.getEpjqd3() != null, ReportSjphbsjBaDetail::getEpjqd3, bo.getEpjqd3());
        lqw.eq(StringUtils.isNotBlank(bo.getShuin()), ReportSjphbsjBaDetail::getShuin, bo.getShuin());
        lqw.eq(StringUtils.isNotBlank(bo.getChanjlmc1()), ReportSjphbsjBaDetail::getChanjlmc1, bo.getChanjlmc1());
        lqw.eq(StringUtils.isNotBlank(bo.getChanjlyl1()), ReportSjphbsjBaDetail::getChanjlyl1, bo.getChanjlyl1());
        lqw.eq(StringUtils.isNotBlank(bo.getChanjlmc2()), ReportSjphbsjBaDetail::getChanjlmc2, bo.getChanjlmc2());
        lqw.eq(StringUtils.isNotBlank(bo.getChanjlyl2()), ReportSjphbsjBaDetail::getChanjlyl2, bo.getChanjlyl2());
        lqw.eq(StringUtils.isNotBlank(bo.getShui()), ReportSjphbsjBaDetail::getShui, bo.getShui());
        lqw.eq(StringUtils.isNotBlank(bo.getSha()), ReportSjphbsjBaDetail::getSha, bo.getSha());
        lqw.eq(StringUtils.isNotBlank(bo.getWaijjmc1()), ReportSjphbsjBaDetail::getWaijjmc1, bo.getWaijjmc1());
        lqw.eq(StringUtils.isNotBlank(bo.getWaijjyl1()), ReportSjphbsjBaDetail::getWaijjyl1, bo.getWaijjyl1());
        lqw.eq(StringUtils.isNotBlank(bo.getWaijjmc2()), ReportSjphbsjBaDetail::getWaijjmc2, bo.getWaijjmc2());
        lqw.eq(StringUtils.isNotBlank(bo.getWaijjyl2()), ReportSjphbsjBaDetail::getWaijjyl2, bo.getWaijjyl2());
        lqw.eq(StringUtils.isNotBlank(bo.getShuinssb()), ReportSjphbsjBaDetail::getShuinssb, bo.getShuinssb());
        lqw.eq(StringUtils.isNotBlank(bo.getShuinl()), ReportSjphbsjBaDetail::getShuinl, bo.getShuinl());
        lqw.eq(StringUtils.isNotBlank(bo.getChanjlmc1l()), ReportSjphbsjBaDetail::getChanjlmc1l, bo.getChanjlmc1l());
        lqw.eq(StringUtils.isNotBlank(bo.getChanjlyl1l()), ReportSjphbsjBaDetail::getChanjlyl1l, bo.getChanjlyl1l());
        lqw.eq(StringUtils.isNotBlank(bo.getChanjlmc2l()), ReportSjphbsjBaDetail::getChanjlmc2l, bo.getChanjlmc2l());
        lqw.eq(StringUtils.isNotBlank(bo.getChanjlyl2l()), ReportSjphbsjBaDetail::getChanjlyl2l, bo.getChanjlyl2l());
        lqw.eq(StringUtils.isNotBlank(bo.getShuil()), ReportSjphbsjBaDetail::getShuil, bo.getShuil());
        lqw.eq(StringUtils.isNotBlank(bo.getShal()), ReportSjphbsjBaDetail::getShal, bo.getShal());
        lqw.eq(StringUtils.isNotBlank(bo.getWaijjmc1l()), ReportSjphbsjBaDetail::getWaijjmc1l, bo.getWaijjmc1l());
        lqw.eq(StringUtils.isNotBlank(bo.getWaijjyl1l()), ReportSjphbsjBaDetail::getWaijjyl1l, bo.getWaijjyl1l());
        lqw.eq(StringUtils.isNotBlank(bo.getWaijjmc2l()), ReportSjphbsjBaDetail::getWaijjmc2l, bo.getWaijjmc2l());
        lqw.eq(StringUtils.isNotBlank(bo.getWaijjyl2l()), ReportSjphbsjBaDetail::getWaijjyl2l, bo.getWaijjyl2l());
        lqw.eq(StringUtils.isNotBlank(bo.getChoud()), ReportSjphbsjBaDetail::getChoud, bo.getChoud());
        lqw.eq(StringUtils.isNotBlank(bo.getBiaogmd()), ReportSjphbsjBaDetail::getBiaogmd, bo.getBiaogmd());
        lqw.eq(bo.getShz1() != null, ReportSjphbsjBaDetail::getShz1, bo.getShz1());
        lqw.eq(bo.getSqd1() != null, ReportSjphbsjBaDetail::getSqd1, bo.getSqd1());
        lqw.eq(bo.getSpjqd1() != null, ReportSjphbsjBaDetail::getSpjqd1, bo.getSpjqd1());
        return lqw;
    }

    /**
     * 新增砂浆配合比设计试验检测记录详情
     */
    @Override
    public Boolean insertByBo(ReportSjphbsjBaDetailBo bo) {
        ReportSjphbsjBaDetail add = BeanUtil.toBean(bo, ReportSjphbsjBaDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改砂浆配合比设计试验检测记录详情
     */
    @Override
    public Boolean updateByBo(ReportSjphbsjBaDetailBo bo) {
        ReportSjphbsjBaDetail update = BeanUtil.toBean(bo, ReportSjphbsjBaDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportSjphbsjBaDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除砂浆配合比设计试验检测记录详情
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
